package com.novellotus.iov.carvice.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.novellotus.iov.carvice.R;
import com.novellotus.iov.carvice.utils.MyUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by Rich on 2017/8/2.
 */

public class FaultExpandableAdapter extends BaseExpandableListAdapter {

    private List<HashMap<String, String>> groupArray;
    private List<List<HashMap<String, String>>> childArray;
    private Context context;
    private int chooseCount = 0;
    private FaultExpandableInterface faultExpandableInterface;

    public FaultExpandableAdapter(List<HashMap<String, String>> groupArray, List<List<HashMap<String, String>>> childArray, Context context, FaultExpandableInterface faultExpandableInterface) {
        this.groupArray = groupArray;
        this.childArray = childArray;
        this.context = context;
        this.faultExpandableInterface = faultExpandableInterface;
    }

    /**
     * @param grounp 当前打开的
     * @param open   0是关闭 1是打开
     */
    public void changeArrow(int grounp, String open) {
        MyUtils.logInformation("打开:" + open);
        groupArray.get(grounp).put("open", open);
        notifyDataSetChanged();
    }

    /**
     * @param grounp
     * @param child
     * @param check
     */
    public void changeCheck(int grounp, int child, String check) {
        faultExpandableInterface.clickCheck(chooseCount);
        childArray.get(grounp).get(child).put("check", check);
        notifyDataSetChanged();
    }

    @Override
    public int getGroupCount() {
        return groupArray.size();
    }

    @Override
    public int getChildrenCount(int i) {
        return childArray.get(i).size();
    }

    @Override
    public Object getGroup(int i) {
        return groupArray.get(i);
    }

    @Override
    public Object getChild(int i, int j) {
        return childArray.get(i).get(j);
    }

    @Override
    public long getGroupId(int i) {
        return i;
    }

    @Override
    public long getChildId(int i, int j) {
        return j;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getGroupView(int i, boolean b, View convertView, ViewGroup viewGroup) {
        View view = convertView;
        GroupHolder holder;
        if (view == null) {
            holder = new GroupHolder();
            view = LayoutInflater.from(context).inflate(R.layout.layout_group_item, viewGroup, false);
            holder.groupName = view.findViewById(R.id.tv_group);
            holder.view_top = view.findViewById(R.id.view_top);
            holder.arrow = view.findViewById(R.id.img_arrow);
            view.setTag(holder);
        } else {
            holder = (GroupHolder) view.getTag();
        }
        if (groupArray.get(i).get("open").equals("0")) {
            holder.arrow.setBackgroundResource(R.mipmap.icon_arrow_up);
        } else {
            holder.arrow.setBackgroundResource(R.mipmap.icon_arrow_down);
        }
        holder.groupName.setText(groupArray.get(i).get("tittle"));
        if (i == 0) {
            holder.view_top.setVisibility(View.GONE);
        } else {
            holder.view_top.setVisibility(View.VISIBLE);
        }
        return view;
    }

    @Override
    public View getChildView(final int grounp, final int grounpChild, boolean b, View convertView, ViewGroup viewGroup) {
        convertView = LayoutInflater.from(context).inflate(R.layout.layout_child_item, viewGroup, false);
        RelativeLayout rlt_child = convertView.findViewById(R.id.rlt_child);
        TextView childName = convertView.findViewById(R.id.tv_child);
        View view_bottom = convertView.findViewById(R.id.view_bottom);
        View view_show_start = convertView.findViewById(R.id.view_show_start);
        final CheckBox checkbox = convertView.findViewById(R.id.checkbox);
        //设置复选框状态
        if (childArray.get(grounp).get(grounpChild).get("check").equals("0")) {
            checkbox.setChecked(false);
        } else {
            checkbox.setChecked(true);
        }
        //点击布局改变复选框状态
        rlt_child.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                checkbox.setChecked(!checkbox.isChecked());
            }
        });

        //设置最后一个子布局横向
        if (grounpChild == childArray.get(grounp).size() - 1) {
            view_show_start.setVisibility(View.GONE);
        } else {
            view_show_start.setVisibility(View.VISIBLE);
        }

        //最后一个子布局显示占位背景
        if (grounp == groupArray.size() - 1 && grounpChild == childArray.get(groupArray.size() - 1).size() - 1) {
            view_bottom.setVisibility(View.VISIBLE);
        } else {
            view_bottom.setVisibility(View.GONE);
        }
        childName.setText(childArray.get(grounp).get(grounpChild).get("tittle"));

        checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()

        {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean choose) {

                if (choose) {
                    if (chooseCount < 10) {
                        checkbox.setChecked(true);
                        chooseCount++;
                        changeCheck(grounp, grounpChild, "1");
                    } else {
                        checkbox.setChecked(false);
                    }

                } else {
                    chooseCount--;
                    changeCheck(grounp, grounpChild, "0");
                }

            }
        });
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int i, int i1) {
        return true;
    }

    private class GroupHolder {
        TextView groupName;
        View view_top;
        ImageView arrow;
    }

    /**
     * @return 获取返回的
     */
    public String getChooseType() {
        StringBuffer str = new StringBuffer();
        for (List<HashMap<String, String>> hashList : childArray) {
            for (HashMap<String, String> hash : hashList) {
                if (!hash.get("check").equals("0")) {
                    if (str.length() == 0) {
                        str.append(hash.get("tittle"));
                    } else {
                        str.append("、");
                        str.append(hash.get("tittle"));
                    }
                }
            }
        }
        return str.toString();
    }

    /**
     * @return 返回选择的故障的key
     */
    public String[] getChooseTypeKey() {
        List<String> list = new ArrayList<>();
        for (List<HashMap<String, String>> hashList : childArray) {
            for (HashMap<String, String> hash : hashList) {
                if (!hash.get("check").equals("0")) {
                    list.add(hash.get("key"));
                }
            }
        }
        String[] strings = new String[list.size()];
        return list.toArray(strings);
    }

    /**
     * @param choose 还原上次选择的故障类型
     */
    public void setChoose(String choose) {
        if (!choose.equals("")) {
            String[] chooseType = choose.split("、");
            for (List<HashMap<String, String>> hashList : childArray) {
                for (HashMap<String, String> hash : hashList) {
                    for (String type : chooseType) {
                        if (hash.get("tittle").equals(type)) {
                            hash.put("check", "1");
                            chooseCount++;
                        }
                    }
                }
            }
            faultExpandableInterface.clickCheck(chooseCount);
            notifyDataSetChanged();
        }
    }

    public interface FaultExpandableInterface {
        void clickCheck(int count);
    }
//
//    private class ChildHolder {
//        TextView childName;
//        CheckBox checkbox;
//    }
}
